<template>
  <c-form-dialog
    ref="form"
    :visible.sync="innerVisible"
    :form-items="formItems"
    :form-data="formData"
    :table-name="tableName"
    :table-id="tableId"
    @close="handleClose"
    @success="handleSuccess"
    :before-save="handleBeforeSave"
  ></c-form-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  components: {},
  props: {
    visible: Boolean,
    tableId: String,
    formData: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    const isNumberValidator = (rule, value, callback) => {
      if (!value) {
        callback()
      } else if (!Number.isFinite(+value) || +value < 0) {
        callback(new Error('请输入正确的数值'))
      } else {
        callback()
      }
    }
    return {
      innerVisible: false,
      tableName: 'industrial_package',
      formItems: [
        {
          tag: 'input',
          itemAttrs: {
            label: '产品名',
            rules: [{ required: true, message: '请输入产品名', trigger: 'blur' }],
          },
          attrs: { key: 'load_name', value: '', placeholder: '请输入产品名' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '担保费率',
            rules: [
              { required: true, message: '请输入担保费率', trigger: 'blur' },
              { validator: isNumberValidator, trigger: 'blur' },
            ],
          },
          attrs: { key: 'gua_rate', value: '', placeholder: '请输入担保费率' },
          tagSlots: [{ name: 'append', slot: '‰' }],
        },
        {
          tag: 'number',
          itemAttrs: {
            label: '有效期',
            rules: [
              { required: true, message: '请输入有效期', trigger: 'blur' },
              { validator: isNumberValidator, trigger: 'blur' },
            ],
          },
          attrs: { key: 'expire_days', value: '', placeholder: '请输入有效期' },
          tagSlots: [{ name: 'append', slot: '月' }],
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '出函时间',
            rules: [
              { required: true, message: '请输入出函时间', trigger: 'blur' },
              { validator: isNumberValidator, trigger: 'blur' },
            ],
          },
          attrs: { key: 'open_hour', value: '', placeholder: '请输入出函时间' },
          tagSlots: [{ name: 'append', slot: '小时' }],
        },

        {
          tag: 'input',
          itemAttrs: {
            label: '最低收取保费',
            rules: [
              { required: true, message: '请输入最低收取保费', trigger: 'blur' },
              { validator: isNumberValidator, trigger: 'blur' },
            ],
          },
          attrs: { key: 'min_gua_fee', value: '', placeholder: '请输入最低收取保费' },
          tagSlots: [{ name: 'append', slot: '元' }],
        },

        {
          tag: 'textarea',
          itemAttrs: {
            label: '备注',
          },
          attrs: { key: 'remark', value: '' },
        },
      ],
    }
  },
  computed: {
    ...mapGetters(['clientId']),
  },
  watch: {
    visible(val) {
      this.innerVisible = val
    },
  },
  created() {},
  mounted() {},
  methods: {
    handleBeforeSave(model) {
      model.supply_agency_id = this.clientId
      model.service_id = this.clientId
      model.is_guarantee = 1
      return model
    },
    handleClose() {
      this.$emit('update:visible', false)
    },
    handleSuccess(res) {
      this.$emit('success', res)
    },
  },
}
</script>

<style lang="scss" scoped></style>
